/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:25 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class DanhmuckiemtraBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Danhmuckiemtra Này";
		private static string EX_STENDANHMUC_EMPTY="sTendanhmuc không được để trống";
		private static string EX_SKYHIEU_EMPTY="sKyhieu không được để trống";
		private static string EX_IHESODIEM_INVALID="iHesodiem không hợp lệ";
		private static string EX_ID_INVALID="PK_iDanhmuckiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Danhmuckiemtra Theo ID
        /// </summary>
        /// <param name="PK_iDanhmuckiemtraID">Int16:Danhmuckiemtra ID</param>
        /// <returns>DanhmuckiemtraEntity</returns>        
        public static DanhmuckiemtraEntity GetOne(Int16 PK_iDanhmuckiemtraID)
        {
            
			if(PK_iDanhmuckiemtraID<=0)
				throw new Exception(EX_ID_INVALID);
            return DanhmuckiemtraDAL.GetOne(PK_iDanhmuckiemtraID);
        }
        /// <summary>
        /// Lấy về List các Danhmuckiemtra
        /// </summary>
        /// <returns>List DanhmuckiemtraEntity:List Danhmuckiemtra Cần lấy</returns>
        public static List<DanhmuckiemtraEntity> GetAll()
        {
            return DanhmuckiemtraDAL.GetAll();
        }
        
        /// <summary>
        /// Kiểm tra và thêm mới Danhmuckiemtra
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Danhmuckiemtra Mới Thêm Vào</returns>
        public static Int32 Add(DanhmuckiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return DanhmuckiemtraDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Danhmuckiemtra
        /// </summary>
        /// <param name="entity">DanhmuckiemtraEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(DanhmuckiemtraEntity entity)
        {
            checkExist(entity.PK_iDanhmuckiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return DanhmuckiemtraDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Danhmuckiemtra
        /// </summary>
        /// <param name="PK_iDanhmuckiemtraID">Int16 : PK_iDanhmuckiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int16 PK_iDanhmuckiemtraID)
        {
            checkExist(PK_iDanhmuckiemtraID);
            return DanhmuckiemtraDAL.Remove(PK_iDanhmuckiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int16 PK_iDanhmuckiemtraID)
        {
            DanhmuckiemtraEntity oDanhmuckiemtra=DanhmuckiemtraDAL.GetOne(PK_iDanhmuckiemtraID);
            if(oDanhmuckiemtra==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">DanhmuckiemtraEntity: entity</param>
        private static void checkLogic(DanhmuckiemtraEntity entity)
        {   
			if (String.IsNullOrEmpty(entity.sTendanhmuc))
				throw new Exception(EX_STENDANHMUC_EMPTY);
			if (String.IsNullOrEmpty(entity.sKyhieu))
				throw new Exception(EX_SKYHIEU_EMPTY);
			if (entity.iHesodiem < 0)
				throw new Exception(EX_IHESODIEM_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">DanhmuckiemtraEntity: DanhmuckiemtraEntity</param>
        private static void checkDuplicate(DanhmuckiemtraEntity entity,bool checkPK)
        {
            /* 
            Example
            List<DanhmuckiemtraEntity> list = DanhmuckiemtraDAL.GetAll();
            if (list.Exists(
                delegate(DanhmuckiemtraEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.PK_iDanhmuckiemtraID != entity.PK_iDanhmuckiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">DanhmuckiemtraEntity:entity</param>
        private static void checkFK(DanhmuckiemtraEntity entity)
        {            
        }
        #endregion
    }
}
